{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2bf3e27b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a5c83745",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>职位名称</th>\n",
       "      <th>详细链接</th>\n",
       "      <th>工作地点</th>\n",
       "      <th>薪资</th>\n",
       "      <th>公司名称</th>\n",
       "      <th>经验要求</th>\n",
       "      <th>学历</th>\n",
       "      <th>福利</th>\n",
       "      <th>职位信息</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>数据分析（GIS方向）</td>\n",
       "      <td>https://www.lagou.com/jobs/6352539.html</td>\n",
       "      <td>杨浦区</td>\n",
       "      <td>15k-20k</td>\n",
       "      <td>上海树融数据科技有限公司</td>\n",
       "      <td>3-5年</td>\n",
       "      <td>硕士</td>\n",
       "      <td>扁平化管理,快速成长,与大牛共事</td>\n",
       "      <td>职责描述：1.主要为城市数据（交通、规划、地理信息、人口、运营商等数据）分析，模型构建、算法...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>数据分析师</td>\n",
       "      <td>https://www.lagou.com/jobs/6352950.html</td>\n",
       "      <td>嘉定区</td>\n",
       "      <td>8k-15k</td>\n",
       "      <td>上海景域文化传播股份有限公司</td>\n",
       "      <td>3-5年</td>\n",
       "      <td>本科</td>\n",
       "      <td>平台佳,提升快,氛围好</td>\n",
       "      <td>工作职责：1、BOSS系统开发；2、数据魔方系统开发3、SQL分析开发；4、数据分析模型开发...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>商业分析师/数据分析师</td>\n",
       "      <td>https://www.lagou.com/jobs/6266665.html</td>\n",
       "      <td>浦东新区</td>\n",
       "      <td>20k-40k</td>\n",
       "      <td>上海阅文信息技术有限公司</td>\n",
       "      <td>3-5年</td>\n",
       "      <td>本科</td>\n",
       "      <td>Top团队,福利好,环境佳,发展快</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>数据部-数据分析师（SH）</td>\n",
       "      <td>https://www.lagou.com/jobs/6349129.html</td>\n",
       "      <td>长宁区</td>\n",
       "      <td>20k-30k</td>\n",
       "      <td>途家网网络技术（北京）有限公司</td>\n",
       "      <td>3-5年</td>\n",
       "      <td>硕士</td>\n",
       "      <td>发展前景好、绩效奖金、期权激励</td>\n",
       "      <td>工作职责：1、通过SQL收集数据，分析流量、活动、订单等核心数据，形成可视化报表；2、分析用...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>数据分析师</td>\n",
       "      <td>https://www.lagou.com/jobs/6349406.html</td>\n",
       "      <td>杨浦区</td>\n",
       "      <td>25k-50k</td>\n",
       "      <td>遨游酒店信息技术(深圳)有限责任公司</td>\n",
       "      <td>5-10年</td>\n",
       "      <td>本科</td>\n",
       "      <td>弹性上下班;团队有趣;晚餐;打车报销</td>\n",
       "      <td>职位职责： 1、负责商业化指标体系建设和完善，通过对业务的敏锐洞察和理解，构建合理的评价体系...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            职位名称                                     详细链接  工作地点       薪资  \\\n",
       "0    数据分析（GIS方向）  https://www.lagou.com/jobs/6352539.html   杨浦区  15k-20k   \n",
       "1          数据分析师  https://www.lagou.com/jobs/6352950.html   嘉定区   8k-15k   \n",
       "2    商业分析师/数据分析师  https://www.lagou.com/jobs/6266665.html  浦东新区  20k-40k   \n",
       "3  数据部-数据分析师（SH）  https://www.lagou.com/jobs/6349129.html   长宁区  20k-30k   \n",
       "4          数据分析师  https://www.lagou.com/jobs/6349406.html   杨浦区  25k-50k   \n",
       "\n",
       "                 公司名称   经验要求  学历                  福利  \\\n",
       "0        上海树融数据科技有限公司   3-5年  硕士    扁平化管理,快速成长,与大牛共事   \n",
       "1      上海景域文化传播股份有限公司   3-5年  本科         平台佳,提升快,氛围好   \n",
       "2        上海阅文信息技术有限公司   3-5年  本科   Top团队,福利好,环境佳,发展快   \n",
       "3     途家网网络技术（北京）有限公司   3-5年  硕士     发展前景好、绩效奖金、期权激励   \n",
       "4  遨游酒店信息技术(深圳)有限责任公司  5-10年  本科  弹性上下班;团队有趣;晚餐;打车报销   \n",
       "\n",
       "                                                职位信息  \n",
       "0  职责描述：1.主要为城市数据（交通、规划、地理信息、人口、运营商等数据）分析，模型构建、算法...  \n",
       "1  工作职责：1、BOSS系统开发；2、数据魔方系统开发3、SQL分析开发；4、数据分析模型开发...  \n",
       "2                                                NaN  \n",
       "3  工作职责：1、通过SQL收集数据，分析流量、活动、订单等核心数据，形成可视化报表；2、分析用...  \n",
       "4  职位职责： 1、负责商业化指标体系建设和完善，通过对业务的敏锐洞察和理解，构建合理的评价体系...  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('拉钩网招聘_数据分析_上海.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "06f5c957",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 处理薪资数据，去掉k，K,将每个范围字符串按 - 分割为列表,取均值\n",
    "salary_ser = df[\"薪资\"].str.replace(\"k\", \"\").str.replace(\"K\", \"\").str.split(\"-\", expand=True).astype({0:float, 1:float}).mean(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c6ad858f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 处理经验\n",
    "experience_ser = df[\"经验要求\"].str.replace(\"应届毕业生\",\"0\").str.replace(\"不限\",\"0\").str.replace(\"年以下\",\"\").str.replace(\"年以上\",\"\").str.replace(\"年\",\"\").str.split(\"-\", expand=True).astype({0:float, 1:float}).mean(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfad9eb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import OneHotEncoder\n",
    "OHE_edu = OneHotEncoder()\n",
    "# 对学历列进行独热编码\n",
    "edu_arr = OHE_edu.fit_transform(df[[\"学历\"]]).toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d284977e",
   "metadata": {},
   "outputs": [],
   "source": [
    "OHE_location = OneHotEncoder()\n",
    "loc_arr = OHE_location.fit_transform(df[[\"工作地点\"]]).toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7b7acc32",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\py\\lib\\site-packages\\sklearn\\base.py:465: UserWarning: X does not have valid feature names, but OneHotEncoder was fitted with feature names\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "OHE_location.transform([['嘉定区']]).toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "15e0df84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4. ,  4. ,  4. ,  4. ,  7.5,  4. ,  0. ,  0. ,  7.5,  0. ,  4. ,\n",
       "        0. ,  4. ,  2. ,  0. ,  0. ,  4. ,  4. ,  4. ,  4. ,  7.5,  2. ,\n",
       "        2. ,  4. ,  4. ,  0. ,  0. ,  0. ,  4. ,  7.5,  7.5,  7.5,  2. ,\n",
       "        2. ,  4. ,  4. ,  4. ,  7.5,  2. ,  2. ,  2. ,  4. ,  4. ,  4. ,\n",
       "        4. ,  4. ,  4. ,  4. ,  2. ,  4. ,  2. ,  4. ,  4. ,  4. ,  4. ,\n",
       "        4. ,  4. ,  0. ,  4. ,  4. ,  4. ,  2. ,  2. ,  4. ,  2. ,  2. ,\n",
       "        4. ,  2. ,  2. ,  4. ,  4. ,  2. ,  4. ,  2. ,  4. ,  4. ,  0. ,\n",
       "        2. ,  4. ,  4. ,  4. ,  4. ,  4. ,  2. ,  4. ,  2. ,  2. ,  2. ,\n",
       "        4. ,  7.5,  4. ,  2. ,  2. ,  7.5,  2. ,  7.5,  2. ,  4. ,  4. ,\n",
       "        0. ,  2. ,  2. ,  2. ,  2. ,  2. ,  4. ,  4. ,  4. ,  4. ,  0. ,\n",
       "        4. ,  0. ,  7.5,  4. ,  2. ,  0. ,  7.5,  2. ,  0. ,  4. ,  4. ,\n",
       "        2. ,  2. ,  7.5,  0. ,  4. ,  2. ,  2. ,  2. ,  4. ,  4. ,  7.5,\n",
       "        2. ,  4. ,  0. ,  2. ,  1. ,  0. ,  2. ,  2. ,  4. ,  0. ,  4. ,\n",
       "        2. ,  4. ,  4. ,  7.5,  0. ,  2. ,  4. ,  4. ,  7.5,  2. ,  4. ,\n",
       "        4. ,  2. ,  2. ,  4. ,  4. ,  4. ,  4. ,  4. ,  2. ,  4. ,  0. ,\n",
       "        4. ,  7.5,  4. ,  2. ,  4. ,  0. ,  4. ,  2. ,  2. ,  4. ,  0. ,\n",
       "        2. ,  4. ,  4. ,  7.5,  7.5,  2. ,  4. ,  4. ,  7.5,  0. ,  4. ,\n",
       "        2. ,  4. ,  4. ,  1. ,  7.5,  7.5,  2. ,  7.5,  0. ,  4. ,  7.5,\n",
       "        4. ,  7.5, 10. ,  4. ,  2. ,  4. ,  4. ,  7.5,  2. ,  4. ,  0. ,\n",
       "        7.5,  7.5,  0. ,  4. ,  4. ,  4. ,  4. ,  0. ,  4. ,  4. ,  4. ,\n",
       "        7.5,  7.5,  2. ,  2. ,  7.5,  4. ,  4. ,  0. ,  4. ,  4. ,  7.5,\n",
       "        4. ,  7.5,  4. ,  4. ,  4. ,  7.5,  4. ,  1. ,  4. ,  4. ,  4. ,\n",
       "        7.5,  4. ,  7.5,  2. ,  0. ,  2. ,  7.5,  2. ,  2. ,  2. ,  0. ,\n",
       "        2. ,  2. ,  2. ,  7.5,  4. ,  7.5,  2. ,  1. ,  0. ,  4. ,  2. ,\n",
       "        4. ,  4. ,  0. ,  2. ,  4. ,  4. ,  1. ,  2. ,  2. ,  4. ,  0. ,\n",
       "        4. ,  4. ,  0. ,  2. ,  4. ,  4. ,  0. ,  2. ,  4. ,  2. ,  4. ,\n",
       "        4. ,  2. ,  2. ,  2. ,  2. ,  0. ,  4. ,  0. ,  0. ,  0. ,  0. ,\n",
       "        0. ,  7.5,  4. ,  4. ,  2. ,  0. ,  2. ,  4. ,  7.5,  0. ,  4. ,\n",
       "        0. ,  0. ,  4. ,  2. ,  0. ,  0. ,  0. ,  7.5,  2. ,  0. ,  0. ,\n",
       "        0. ,  0. ,  2. ,  4. ,  0. ,  0. ,  7.5,  2. ,  0. ,  7.5,  4. ,\n",
       "        7.5,  4. ,  0. ,  4. ,  0. ,  0. ,  4. ,  4. ,  4. ,  2. ,  7.5,\n",
       "        7.5,  4. ,  0. ,  0. ,  2. ,  2. ,  0. ,  4. ,  4. ,  0. ,  4. ,\n",
       "        2. ,  4. ,  0. ,  2. ,  2. ,  4. ,  2. ,  4. ,  4. ,  2. ,  4. ,\n",
       "        4. ,  2. ,  7.5,  7.5,  2. ,  2. ,  4. ,  4. ,  4. ,  4. ,  4. ,\n",
       "        4. ,  7.5,  4. ,  0. ,  4. ,  4. ,  1. ,  4. ,  2. ,  2. ,  4. ,\n",
       "        2. ,  4. ,  7.5,  2. ,  4. ,  4. ,  2. ,  4. ,  2. ,  2. ,  2. ,\n",
       "        2. ,  4. ,  4. ,  4. ,  4. ,  7.5,  0. ,  0. ,  2. ,  4. ,  4. ,\n",
       "        4. ,  7.5,  2. ,  0. ,  4. ,  7.5,  0. ,  4. ,  7.5,  0. ,  4. ,\n",
       "        4. ,  4. ,  2. ,  2. ,  4. ,  4. ,  4. ,  1. ,  4. ,  4. ,  4. ,\n",
       "        4. ,  7.5,  4. ,  4. ,  4. ,  2. ,  4. ,  4. ,  4. ,  4. ,  7.5,\n",
       "        4. ,  4. ,  0. ,  7.5,  4. ,  7.5,  4. ,  7.5,  0. ,  7.5])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "experience_ser.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "39e2c8e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# NumPy 默认使用 64 位浮点数（float64），即使数值为零，也会以浮点数格式存储。科学计数法是浮点数的标准显示方式\n",
    "# NumPy 为保持数组中所有元素的显示一致性，即使部分元素为零，也会采用相同的科学计数法格式。\n",
    "data = np.concatenate([edu_arr, loc_arr, experience_ser.values.reshape(-1, 1), salary_ser.values.reshape(-1, 1)*1000], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "971a50a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = data[:, :-1]\n",
    "y = data[:, -1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "67e5b10a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2a6ade62",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "906102b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-1 {color: black;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>LinearRegression()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">LinearRegression</label><div class=\"sk-toggleable__content\"><pre>LinearRegression()</pre></div></div></div></div></div>"
      ],
      "text/plain": [
       "LinearRegression()"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "LR = LinearRegression()\n",
    "LR.fit(X_train, y_train) #拟合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f144c7bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  949.01423925, -2285.20439142,   247.93586105,  1088.25429113,\n",
       "       -6386.44116299,  -774.50388132,  -179.04753698, -2988.25715186,\n",
       "        -357.21642584, 12503.1439172 ,  1277.58231583,  2453.0051607 ,\n",
       "        -242.72814686,  1350.13029057, -5841.87100634,   353.51059214,\n",
       "        -761.25872422,  -406.04824002,  2739.95175514])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LR.coef_ #系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "5e5c2df4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7285.1506588213715"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import mean_squared_error, r2_score\n",
    "np.sqrt(mean_squared_error(y_test, LR.predict(X_test)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7aaa594",
   "metadata": {},
   "source": [
    "不限 ----> [1, 0, 0, 0]\n",
    "杨浦区 ----> [0, 0, 0, 0, 1, .....]\n",
    "1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "99957469",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\py\\lib\\site-packages\\sklearn\\base.py:465: UserWarning: X does not have valid feature names, but OneHotEncoder was fitted with feature names\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "my_edu = OHE_edu.transform([['不限']]).toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "0758c4d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\py\\lib\\site-packages\\sklearn\\base.py:465: UserWarning: X does not have valid feature names, but OneHotEncoder was fitted with feature names\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "my_location = OHE_location.transform([['杨浦区']]).toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "5fbf6367",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_location"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "069ed2ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_data = np.concatenate([my_edu, my_location, [[float(1)]]], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "028e4064",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([14260.44784994])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LR.predict(my_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "6c6e18d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c727fa14",
   "metadata": {},
   "source": [
    "模型持久化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "bdce14da",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('LR.pkl', 'wb') as file:\n",
    "    pickle.dump(LR, file)\n",
    "with open('OHE_location.pkl', 'wb') as file:\n",
    "    pickle.dump(OHE_location, file)\n",
    "with open('OHE_edu.pkl', 'wb') as file:\n",
    "    pickle.dump(OHE_edu, file)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ccffd25",
   "metadata": {},
   "source": [
    "加载模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "780fd077",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('LR.pkl', 'rb') as file:\n",
    "    LR = pickle.load(file)\n",
    "with open('OHE_location.pkl', 'rb') as file:\n",
    "    OHE_location = pickle.load(file)\n",
    "with open('OHE_edu.pkl', 'rb') as file:\n",
    "    OHE_edu = pickle.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "7056880c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-4 {color: black;}#sk-container-id-4 pre{padding: 0;}#sk-container-id-4 div.sk-toggleable {background-color: white;}#sk-container-id-4 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-4 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-4 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-4 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-4 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-4 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-4 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-4 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-4 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-4 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-4 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-4 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-4 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-4 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-4 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-4 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-4 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-4 div.sk-item {position: relative;z-index: 1;}#sk-container-id-4 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-4 div.sk-item::before, #sk-container-id-4 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-4 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-4 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-4 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-4 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-4 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-4 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-4 div.sk-label-container {text-align: center;}#sk-container-id-4 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-4 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-4\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>OneHotEncoder()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-4\" type=\"checkbox\" checked><label for=\"sk-estimator-id-4\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">OneHotEncoder</label><div class=\"sk-toggleable__content\"><pre>OneHotEncoder()</pre></div></div></div></div></div>"
      ],
      "text/plain": [
       "OneHotEncoder()"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "OHE_location"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8c6d2cd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
